﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;

namespace JM.JCY.SharePointComponent.PersonalHome.ViewMyDocument
{
    public partial class ViewMyDocumentUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string userName = SPContext.Current.Web.CurrentUser.Name;

            SPWeb myWeb = SPContext.Current.Web;
            if (IsExistList(myWeb, userName))
            {
                SPList docLibrary = myWeb.Lists[userName];
                Microsoft.SharePoint.WebControls.ListView lv = new Microsoft.SharePoint.WebControls.ListView();
                lv.ListId = docLibrary.ID.ToString();
                this.divPersonal.Controls.Add(lv);

                string onclick = string.Format("javascript:NewItem2(event, '{0}');javascript:return false;", docLibrary.DefaultNewFormUrl);
                this.idHomePageNewItem.Attributes.Add("onclick", onclick);
            }

        }

        private bool IsExistList(SPWeb web, string listName)
        {
            bool isExist = false;
            SPListCollection lists = web.Lists;
            foreach (SPList item in lists)
            {
                if (item.Title.Equals(listName))
                {
                    isExist = true;
                    break;
                }

            }

            return isExist;
       }
    }
}
